#include <string.h>
#include <stdio.h>
#include <io.h>

#include <dev/board.h>
#include <sys/timer.h>

static char *banner = "\nNut/OS UART Sample\n";
static prog_char presskey_P[] = "Press any key...";
static prog_char pgm_ptr[] = "\nHello stranger!\n";

static char inbuf[128];

/*
 * UART sample.
 *
 * Some functions do not work with ICCAVR.
 */
int main(void)
{
    int got;
    int i;
    char *cp;
    u_long baud = 115200;
    FILE *uart;

    NutRegisterDevice(&DEV_UART, 0, 0);

    freopen(DEV_UART_NAME, "w", stdout);
    freopen(DEV_UART_NAME, "r", stdin);

    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);


    /*
     * Nut/OS never expects a thread to return. So we enter an
     * endless loop here.
     */
    for (i = 0;; i++) {
        /*
         * A bit more advanced input routine is able to read a string
         * up to and including the first newline character or until a
         * specified maximum number of characters, whichever comes first.
         */
        printf("\nEnter your name: ");
        fflush(stdout);
        fgets(inbuf, sizeof(inbuf), stdin);

        /*
         * Chop off trailing linefeed.
         */
        cp = strchr(inbuf, '\n');
        if (cp)
            *cp = 0;

        /*
         * Streams support formatted output as well as printing strings
         * from program space.
         */
        if (inbuf[0])
            printf("\nHello %s!\n", inbuf);
    }
    return 0;
}
